package com.lcj.springems.dao.impl;

import com.lcj.springems.dao.EmpDao;
import com.lcj.springems.entity.Emp;
import com.lcj.springems.exception.DataAccessException;
import com.lcj.springems.rowmapper.EmpRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class EmpDaoImpl extends JdbcDaoSupport implements EmpDao {

    @Override
    public void insert(Emp emp) throws DataAccessException {
        String sql = new StringBuffer()
                .append("insert into t_emp")
                .append("(name,salary,gender,edu,hobbies) ")
                .append(" values ")
                .append("(?,?,?,?,?)").toString();
        try {
            getJdbcTemplate().update(sql,emp.getName(),emp.getSalary(),emp.getGender(),emp.getEdu(),emp.getHob());
        } catch (org.springframework.dao.DataAccessException e) {
            e.printStackTrace();
            throw new DataAccessException(e.getMessage());
        }
    }

    @Override
    public void delete(Integer id) throws DataAccessException {
        String sql = new StringBuffer()
                .append("delete from t_emp where id = ?").toString();
        try {
            getJdbcTemplate().update(sql,id);
        } catch (org.springframework.dao.DataAccessException e) {
            e.printStackTrace();
            throw new DataAccessException(e.getMessage());
        }
    }

    @Override
    public void update(Emp emp) throws DataAccessException {
        String sql = new StringBuffer()
                .append("update t_emp set")
                .append(" name=?, ")
                .append(" salary=?, ")
                .append(" gender=?, ")
                .append(" edu=?, ")
                .append(" hobbies=? ")
                .append(" where id =?").toString();
        try {
            getJdbcTemplate().update(sql,emp.getName(),emp.getSalary(),emp.getGender(),emp.getEdu(),emp.getHob(),emp.getId());
        } catch (org.springframework.dao.DataAccessException e) {
            e.printStackTrace();
            throw new DataAccessException(e.getMessage());
        }
    }

    @Override
    public Emp selectById(Integer id) throws DataAccessException {
        String sql = new StringBuffer()
                .append("select * from t_emp where id = ?").toString();
        List<Emp> list = null;
        try {
            list = getJdbcTemplate().query(sql, new Object[]{id}, new EmpRowMapper());
        } catch (org.springframework.dao.DataAccessException e) {
            e.printStackTrace();
            throw new DataAccessException(e.getMessage());
        }
        return list.isEmpty()?null:list.get(0);
    }

    @Override
    public List<Emp> selectAll() throws DataAccessException {
        String sql = new StringBuffer()
                .append("select * from t_emp").toString();
        List list = null;
        try {
            list = getJdbcTemplate().query(sql, new EmpRowMapper());
        } catch (org.springframework.dao.DataAccessException e) {
            e.printStackTrace();
            throw new DataAccessException(e.getMessage());
        }
        return list;
    }
}
